#include "sms.h"

static gboolean theme_load_fonts(xmlNode *a_node,Theme *theme)
{
	xmlNode *cur_node = NULL;
	GdkPixbuf*  pixbuf;
	char pic[256];
	int id,i;
	xmlChar * val;
	
	if(!a_node)
		goto err;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"font")==0)
			{			
				val = xmlGetProp (cur_node,(xmlChar*)"id");
				if(!val)
					continue;
				id=atoi(val);
				if(id<0||id>THEME_COLOR_COUNT||theme->color[id])
					continue;
				
				val = xmlGetProp (cur_node,(xmlChar*)"val");
				if(!val)
					continue;
				
				theme->font[id]=g_strdup(val);
			}
		}
	}
	
	for(i=0;i<THEME_FONT_COUNT;i++)
		if(!theme->font[id])
			goto err;
	
	return TRUE;
err:
		fprintf(stderr,"%s:error.\n",__FUNCTION__);
	return FALSE;
}

static gboolean theme_load_colors(xmlNode *a_node,Theme *theme)
{
	xmlNode *cur_node = NULL;
	GdkPixbuf*  pixbuf;
	char pic[256];
	int id,i;
	xmlChar * val;
	
	if(!a_node)
		goto err;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"color")==0)
			{			
				val = xmlGetProp (cur_node,(xmlChar*)"id");
				if(!val)
					continue;
				id=atoi(val);
				if(id<0||id>THEME_COLOR_COUNT||theme->color[id])
					continue;
				
				val = xmlGetProp (cur_node,(xmlChar*)"val");
				if(!val)
					continue;
				
				theme->color[id]=g_strdup(val);
			}
		}
	}
	
	for(i=0;i<THEME_COLOR_COUNT;i++)
		if(!theme->color[id])
			goto err;
	
	return TRUE;
err:
	fprintf(stderr,"%s:error.\n",__FUNCTION__);
	return FALSE;
}

static gboolean theme_load_images(xmlNode *a_node,Theme *theme)
{
	xmlNode *cur_node = NULL;
	GdkPixbuf*  pixbuf;
	char pic[256];
	int w,h,id,i;
	xmlChar * val;
	
	if(!a_node)
		goto err;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"image")==0)
			{			
				w=sms_get_xml_int(cur_node,(xmlChar*)"w",0);
				h=sms_get_xml_int(cur_node,(xmlChar*)"h",0);
				
				val = xmlGetProp (cur_node,(xmlChar*)"id");
				if(!val)
					continue;
				id=atoi(val);
				if(id<0||id>THEME_PIC_COUNT||theme->pic[id])
					continue;
				
				val = xmlGetProp (cur_node,(xmlChar*)"pic");
				if(!val)
					continue;
				
				sprintf(pic,"%s/%s/theme/%s",SMS_RESOURCE_DIR,theme->name,val);
				//fprintf(stderr,"pic=%s\n",pic);
				pixbuf=sms_load_pic(pic,w,h);
				if(!pixbuf)
					continue;
				theme->pic[id]=pixbuf;
				
			}
		}
	}
	
	for(i=0;i<THEME_PIC_COUNT;i++)
		if(!theme->pic[id])
			goto err;
	
	return TRUE;
err:
		fprintf(stderr,"%s:error.\n",__FUNCTION__);
		return FALSE;
}

static gboolean theme_travel_xml(xmlNode *a_node,Theme *theme)
{
	xmlNode *cur_node = NULL;
	
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"fonts")==0)
			{
				if(!theme_load_fonts(cur_node->children,theme))
					return FALSE;
			}
			else if(strcmp(cur_node->name,"colors")==0)
			{
				if(!theme_load_colors(cur_node->children,theme))
					return FALSE;
			}
			else if(strcmp(cur_node->name,"images")==0)
			{
				if(!theme_load_images(cur_node->children,theme))
					return FALSE;
			}
		}
	}

	return TRUE;
}

Theme * theme_new(char *name)
{
	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	char buf[256];
	Theme *theme;
	
	
	
	sprintf(buf,"%s/%s/theme/theme.xml",SMS_RESOURCE_DIR,name);
	
	doc = xmlReadFile(buf, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", buf);
		return NULL;
	}

	root_element = xmlDocGetRootElement(doc);
	
	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
		{
			theme=(Theme *)g_malloc0(sizeof(Theme)+1);
			theme->name=g_strdup(name);
			ret=theme_travel_xml(cur_node->children,theme);
			if(!ret)
				theme_destroy(theme);
		}
	}
	
	xmlFreeDoc(doc);
	xmlCleanupParser();
	if(ret)
		return theme;
	
	return NULL;
}

void theme_destroy(Theme *theme)
{
	int i;
	if(!theme)
		return;
	if(theme->name)
		g_free(theme->name);
	for(i=0;i<THEME_COLOR_COUNT;i++)
	{
		if(theme->color[i])
			g_free(theme->color[i]);
	}
	
	for(i=0;i<THEME_PIC_COUNT;i++)
	{
		if(theme->pic[i])
			g_object_unref(theme->pic[i]);
	}
	
	g_free(theme);
}

const GdkPixbuf *theme_get_pic(Theme * theme,THEME_PIC_ID id)
{
	if(id>=THEME_PIC_COUNT || id<0)
		return NULL;
	
	if(!theme->pic[id])
	
		return (const GdkPixbuf *)theme->pic[id];
}

const char *theme_get_color(Theme * theme,THEME_COLOR_ID id)
{
	if(id>=THEME_COLOR_COUNT || id<0)
		return NULL;
	return (const char *)theme->color[id];
}

const char *theme_get_font(Theme * theme,THEME_FONT_ID id)
{
	if(id>=THEME_FONT_COUNT || id<0)
		return NULL;
	return (const char *)theme->font[id];
}
